package cn.yuhe.system.service.impl;

import cn.yuhe.system.domain.Role;
import cn.yuhe.system.mapper.RoleMapper;
import cn.yuhe.system.service.IRoleService;
import cn.yuhe.basic.service.impl.BaseServiceImpl;
import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuhe
 * @since 2022-07-11
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;


    /*
    * @Title: save
    * @Description: 角色添加
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/7/13 15:09
    * @Parameters:
    * @Return
    */
    @Override
    public void save(Role role) {
        roleMapper.save(role);
        roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        roleMapper.saveRolePermission(role.getId(),role.getPermissions());
    }



    @Override
    public void remove(Long id) {
        roleMapper.remove1(id);
        roleMapper.remove2(id);
        roleMapper.remove(id);
    }

    /*
    * @Title: update
    * @Description: 角色修改
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/7/13 11:46
    * @Parameters:
    * @Return
    */
    @Override
    public void update(Role role) {
        // 修改role表
        roleMapper.update(role);
        // 同步修改其他表
        initRelationTable(role);
    }

    /*
    * @Title: initRelationTable
    * @Description: 数据关联初始化方法
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/7/13 13:06
    * @Parameters:
    * @Return
    */
    private void initRelationTable(Role role) {
        // 先根据role的id删除中间表角色数据，在新增
        roleMapper.removeRoleMenu(role.getId());
        roleMapper.saveRoleMenu(role.getId(), role.getMenus());

        // 先根据role的id删除中间表权限数据，在新增
        roleMapper.removeRolePermission(role.getId());
        roleMapper.saveRolePermission(role.getId(), role.getPermissions());
    }

    /*
    * @Title: initRelationTable2
    * @Description: 新增初始化方法
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/7/13 15:02
    * @Parameters:
    * @Return
    */
    private void initRelationTable2(Role role) {

    }

}
